package com.infinitus.activity.web;

import com.infinitus.activity.pojo.DTO.WeChatReturnDto;
import com.infinitus.activity.pojo.VO.ResponseVO;
import com.infinitus.activity.util.KeyGenerator;
import com.infinitus.activity.util.MD5Util;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @author: luzhenjie
 * @create: 2019/3/11
 * @desc:
 */
@Controller
@RequestMapping(value = "/sys")
public class LoginController {

    private static Logger logger = Logger.getLogger(LoginController.class);

    @Value("${ydapi.env}")
    private String ydapiEnv;

    @Value("${deploycolId}")
    private String deploycolId;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @Desc 用户登录
     */
    @RequestMapping(value = "/login" , method = RequestMethod.POST)
    public ResponseEntity<?> login(@RequestParam String dealerNo ,@RequestParam String password) {
        /**
         * @Desc 卡号格式合法性校验
         */
        if(!Pattern.matches("^\\d{9}$", dealerNo)) {
            return ResponseEntity.badRequest().body(40001);
        }

        try {
            //http://ycapi-test.infinitus.com.cn/ydapi/annualmeeting/Login.do?dealerNo=330754988&password=0192023a7bbd73250516f069df18b500&systemInfo=LUCKY&token=cbfcef79b006537e0c237920d1fc1d10
            //token生成
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String token = MD5Util.string2MD5("infinitus" + dealerNo + "infinitus" + sdf.format(new Date()));
            //String url = ydapiEnv + "?dealerNo=" + dealerNo + "&password=" + password + "&systemInfo=LUCKY&token=" + token;
            /*String url = "https://ydapi-dev.infinitus.com.cn/front/ydapi/dealer/dealer/login?dealerNo=" + dealerNo + "&password=" + password + "&systemInfo=LUCKY&token=" + token;
            logger.error("url:" + url);
            ResponseEntity<WeChatReturnDto> returnDto = new RestTemplate().postForEntity(url, null, WeChatReturnDto.class);
            if (returnDto.getStatusCode().equals(HttpStatus.OK)) {
                //返回参数
                if (returnDto.getBody() != null) {
                    if ("200".equals(returnDto.getBody().getResult())) {
                        Boolean hasKey = stringRedisTemplate.hasKey(KeyGenerator.getStringAlready(dealerNo));
                        if (!hasKey) {
                            stringRedisTemplate.opsForValue().set(KeyGenerator.getStringAlready(dealerNo), "0");
                        }
                        return ResponseEntity.ok().build();
                    }
                }
            }
            return ResponseEntity.badRequest().body("密码不对");*/

            //测试可以用以下代码，账号:123456789  密码:9528
            /*String s = MD5Util.string2MD5("9528");
            if(dealerNo.equals("123456789")&&password.equals(s)) {
                stringRedisTemplate.opsForValue().set(KeyGenerator.getStringAlready(dealerNo),"0");
                return ResponseEntity.ok().build();
            }else {
                return ResponseEntity.badRequest().body("密码不对");
            }*/

            stringRedisTemplate.opsForValue().set(KeyGenerator.getStringAlready(deploycolId,dealerNo),"0");
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e);
        }

    }
}